**************************************************
********* Table 2: Replacement and Maintenance****
********* of Latinas and Latinos in MMD Seats ****
********* Legislative Candidacy in Majority ******
********* Minority Districts *********************
**************************************************

do "do_files/prep_GRACE_data/load_prep_GRACE_data.do"

* Need this line to get reproducible results based on bysort
set sortseed 1234

* generate a variable that indicates whether a latino or latina was the previous winner 
* this only works for single member districts, the whole idea of "replacement" doesn't work the same way for multimember districts 
* first create variable that identifies each unique seat
egen seats_2021 = group(v03 v07 v09 v08 v10a v10b v10c), missing

* then id whether a latina or latino or both have ever won that seat in the  period from 1996-2015. Also count the number of total wins.
gen latm_winner=1 if basket_latm==1 & v24==1
egen seats_2021latm = total(latm_winner), by(seats_2021)

gen latw_winner=1 if basket_latw==1 & v24==1
egen seats_2021latw = total(latw_winner), by(seats_2021)

* this needed for the next step where we generate the MMD seat transitions
order seats_2021 v03 v07 v08 v09 latw_winner latm_winner v05
 
* individual level variable capturing change/no change in winner from one category to another
bysort seats_2021: gen latinatolatino=1 if (latm_winner - latw_winner[_n-1])==0
replace latinatolatino=0 if latinatolatino==.

bysort seats_2021:gen latinotolatina=1 if (latw_winner - latm_winner[_n-1])==0
replace latinotolatina=0 if latinotolatina==.

bysort seats_2021:gen latinotolatino=1 if (latm_winner - latm_winner[_n-1])==0
replace latinotolatino=0 if latinotolatino==.

bysort seats_2021:gen latinatolatina=1 if (latw_winner - latw_winner[_n-1])==0
replace latinatolatina=0 if latinatolatina==.

*create a variable to indicate change from one unique latino to another, and same for latinas
bysort seats_2021:gen uniq_latinotolatino=1 if (latm_winner - latm_winner[_n-1])==0 & (graceuniqid - graceuniqid[_n-1])!=0
replace uniq_latinotolatino=0 if uniq_latinotolatino==.

bysort seats_2021:gen uniq_latinatolatina=1 if (latw_winner - latw_winner[_n-1])==0 & (graceuniqid - graceuniqid[_n-1])!=0
replace uniq_latinatolatina=0 if uniq_latinatolatina==.

egen uniq_latinotolatino_total = total(uniq_latinotolatino), by(seats_2021)
egen uniq_latinatolatina_total = total(uniq_latinatolatina), by(seats_2021)

* Calculate values for table 2 and store them in matrices

* latw -> latm
quietly estpost tabstat latinatolatino if v12==1 & postred2000 == 1 & ballot_mmd == 1, stat(sum)
mat latinatolatino_2000 = e(sum)'

quietly estpost tabstat latinatolatino if v12==1 & postred2010 == 1 & ballot_mmd == 1, stat(sum)
mat latinatolatino_2010 = e(sum)'

quietly estpost tabstat latinatolatino if v12==1 & postred2000 == 0 & postred2010 == 0 & ballot_mmd == 1, stat(sum)
mat latinatolatino_other = e(sum)'

quietly estpost tabstat latinatolatino if v12==1 & ballot_mmd == 1, stat(sum)
mat latinatolatino_total = e(sum)'

mat combined_latinatolatino = latinatolatino_2000 \ latinatolatino_2010 \ latinatolatino_other \ latinatolatino_total

matrix list combined_latinatolatino

* latm -> latw
quietly estpost tabstat latinotolatina if v12==1 & postred2000 == 1 & ballot_mmd == 1, stat(sum)
mat latinotolatina_2000 = e(sum)'

quietly estpost tabstat latinotolatina if v12==1 & postred2010 == 1 & ballot_mmd == 1, stat(sum)
mat latinotolatina_2010 = e(sum)'

quietly estpost tabstat latinotolatina if v12==1 & postred2000 == 0 & postred2010 == 0 & ballot_mmd == 1, stat(sum)
mat latinotolatina_other = e(sum)'

quietly estpost tabstat latinotolatina if v12==1 & ballot_mmd == 1, stat(sum)
mat latinotolatina_total = e(sum)'

mat combined_latinotolatina = latinotolatina_2000 \ latinotolatina_2010 \ latinotolatina_other \ latinotolatina_total

matrix list combined_latinotolatina

* latw -> latw
quietly estpost tabstat uniq_latinatolatina if v12==1 & postred2000 == 1 & ballot_mmd == 1, stat(sum)
mat uniq_latinatolatina_2000 = e(sum)'

quietly estpost tabstat uniq_latinatolatina if v12==1 & postred2010 == 1 & ballot_mmd == 1, stat(sum)
mat uniq_latinatolatina_2010 = e(sum)'

quietly estpost tabstat uniq_latinatolatina if v12==1 & postred2000 == 0 & postred2010 == 0 & ballot_mmd == 1, stat(sum)
mat uniq_latinatolatina_other = e(sum)'

quietly estpost tabstat uniq_latinatolatina if v12==1 & ballot_mmd == 1, stat(sum)
mat uniq_latinatolatina_total = e(sum)'

mat combined_uniq_latinatolatina = uniq_latinatolatina_2000 \ uniq_latinatolatina_2010 \ uniq_latinatolatina_other \ uniq_latinatolatina_total

matrix list combined_uniq_latinatolatina

* latm -> latm
quietly estpost tabstat uniq_latinotolatino if v12==1 & postred2000 == 1 & ballot_mmd == 1, stat(sum)
mat uniq_latinotolatino_2000 = e(sum)'

quietly estpost tabstat uniq_latinotolatino if v12==1 & postred2010 == 1 & ballot_mmd == 1, stat(sum)
mat uniq_latinotolatino_2010 = e(sum)'

quietly estpost tabstat uniq_latinotolatino if v12==1 & postred2000 == 0 & postred2010 == 0 & ballot_mmd == 1, stat(sum)
mat uniq_latinotolatino_other = e(sum)'

quietly estpost tabstat uniq_latinotolatino if v12==1 & ballot_mmd == 1, stat(sum)
mat uniq_latinotolatino_total = e(sum)'

mat combined_uniq_latinotolatino = uniq_latinotolatino_2000 \ uniq_latinotolatino_2010 \ uniq_latinotolatino_other \ uniq_latinotolatino_total

matrix list combined_uniq_latinotolatino


* Pull results into one table
mat table_2_full_results = combined_latinatolatino, combined_latinotolatina, combined_uniq_latinatolatina, combined_uniq_latinotolatino
mat rownames table_2_full_results = "Post-Redistricting (2000)" "Post-Redistricting (2010)" "All Other Years" "Total"
mat colnames table_2_full_results = "Latw-> Latm" "Latm -> Latw" "Latw -> Latw" "Latm -> Latm"

esttab matrix(table_2_full_results)

* pausing for users who might be looping through all replication files
pause

